﻿var HyneticTpme = new Function();
var baiDuMaps = new Function();
var baidu = new Function();
var geoc = null;
HyneticTpme.prototype.doc = document;
HyneticTpme.prototype.maps = new baiDuMaps();
var Hynetic = new HyneticTpme();



///////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
/////////////////map///////////////////////////
///////////////////////////////////////////////

baiDuMaps.prototype.loadJScript = function (key) {
    if (key == null || key == '') {
        HyneticTpme.prototype.showMsgTips('百度地图加载失败');
        return false;
    }
    var content = document.createElement("script");
    content.src = "http://api.map.baidu.com/api?v=2.0&ak=" + key + "&callback=hyneticMap";
    document.body.appendChild(content);//把script加载到body的最后一行
}

//这里可以根据浏览器的经纬度定位到当前城市,cityName即是当前城市
baiDuMaps.prototype.getMyCity = function () {
    var myCity = new BMap.LocalCity();
    myCity.get(myFun);//获取当前地址
    function myFun(result) {//查询用户当前地址
        cityName = result.name;//获取地址名字
        map.setCenter(cityName);//根据位置定位到现在的城市
        console.log('您现在所在的位置是:' + cityName);
    }//myFun
}//getMyCity()

///添加地图折线
/*
* @arrList : format: [[xxx.xxxxx,xx.xxxx],[xxx.xxxx,xx.xxxx]];
* @flag    : format: 1 : lin  ,  2 : gon
**/
baiDuMaps.prototype.addPolyList = function (arrList, flag, color, borderWidth, opacity) {
    color = color || "#7c78b5";
    borderWidth = borderWidth || 4;
    opacity = opacity || 1;
    //清楚遮盖物
    map.clearOverlays();
    for (var i = 0; i < arrList.length; i++) {
        var list = [];
        for (var j = 0; j < arrList[i].length; j++) {
            list[j] = new BMap.Point(arrList[i][j][0], arrList[i][j][1]);
        }
        var polygon = null;
        //创建折线
        if (flag == 1) {
            polygon = new BMap.Polyline(list, {strokeColor: "#7c78b5", strokeWeight: 4, strokeOpacity: 1});  //创建折线
        } else if (flag = 2) {
            //创建多编写
            polygon = new BMap.Polygon(list, {strokeColor: "#f1dd84", strokeWeight: 4, strokeOpacity: 1});  //创建多边形
        }
        //遮盖物添加到地图
        map.addOverlay(polygon);           //增加多边形
    }//for
}//addPolyList()


//创建标注点,标注点信息为[{id:id,title:标题,longitude:经度,latitude:纬度}]
//根据数组创建标点,bean标注点信息,colorPath标注点图片地址,markerFlag是否删除当前所有的标注点重新添加,默认是不删除
baiDuMaps.prototype.createMapMark = function (bean, addClickHandler, colorPath, markerFlag) {
    markerFlag = markerFlag || 1;
    //colorPath = colorPath || 'blue';
    if (markerFlag == 1) {
        var cMap = map.getOverlays();
        for (var i = 0, len = cMap.length; i < len; i++) {//循环地图
            var flag = cMap[i].toString();//把map转换成字符换以便遍历判断
            if (flag == '[object Marker]') {//如果地图中有标注点,则删除他
                map.removeOverlay(cMap[i]);
            }//if(flag == '[object Marker]')
        }//for(var i = 0 , len = cMap.length ; i < len; i++)
    }//if(markerFlag == 1)
    //遍历标注点信息
    var longitude = null;
    var latitude = null;
    for (var i = 0, len = bean.length; i < len; i++) {
        var tpmeBean = bean[i];
        var title = bean[i].title;
        var topPoint = null;
        if (null != bean[i].point) {
            topPoint = bean[i].point;
            longitude = topPoint.lng;
            latitude = topPoint.lat;
        }else {
            longitude = bean[i].longitude;
            latitude = bean[i].latitude;
        }
        topPoint = new BMap.Point(longitude, latitude);
        if (latitude == null
            || longitude == null || parseFloat(longitude) < 72.1
            || parseFloat(longitude) > 137 || parseFloat(latitude) < 1
            || parseFloat(latitude) > 55) {
            return false;//如果经纬度不合法则返回false
        }
        var marker = null;
        var myIcon = null;
        if (null == colorPath) {//如果没有指定标注点的话,则是默认的标注点
            marker = new BMap.Marker(topPoint);  // 创建标注点
        } else {
            myIcon = new BMap.Icon(colorPath, new BMap.Size(24, 32)); //添加标注点图标
            marker = new BMap.Marker(topPoint, {icon: myIcon});  // 创建标注点
        }
        var label = new BMap.Label(title,{offset:new BMap.Size(20,-10)});
        marker.setLabel(label);
        marker.setTitle(title); //设置标注点显示信息
        //		marker.setTop(true);//设置小区的坐标点气泡为最顶层

        map.addOverlay(marker);	//添加标注点
        //如果传入的方法名称对应的话
        if (typeof addClickHandler == 'function') {
            addClickHandler(marker, tpmeBean);// 给标注点添加事件
        }//if
    }//for
    _longitude = longitude;
    _latitude = latitude;

}//createMapMark()


//根据经纬度获取当前城市地址
baiDuMaps.prototype.getGeolocationCityName = function (fun) {
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            var mPoint = new BMap.Point(r.point.lng, r.point.lat);
            geoc.getLocation(mPoint, function (rs) {
                //获取到位置信息
                var addComp = rs.addressComponents;
                var reqList = {};
                reqList['city'] = addComp.city;
                reqList['district'] = addComp.district;
                reqList['street'] = addComp.street;
                reqList['streetNumber'] = addComp.streetNumber;
                reqList['point'] = mPoint;
                if (typeof fun == 'function') {
                    fun(reqList);
                }
            });

        }
        else {
            console.log('failed' + this.getStatus());
        }
    }, {enableHighAccuracy: true});
}


//定位当前目标
baiDuMaps.prototype.getGeolocation = function (fun) {
    var geolocation = new BMap.Geolocation();
    geolocation.getCurrentPosition(function (r) {
        if (this.getStatus() == BMAP_STATUS_SUCCESS) {
            var mPoint = new BMap.Point(r.point.lng, r.point.lat);
            //var mPoint = new BMap.Point(121.453889,31.262863);
            map.panTo(mPoint);
            map.setZoom(19);
            if (typeof fun == 'function') {
                fun(geoc, mPoint);
            }
        }
        else {
            console.log('failed' + this.getStatus());
        }
    }, {enableHighAccuracy: true});
}//getGeolocation()
//根据当前目标查询附近poi点
baiDuMaps.prototype.getGeocoder = function (mPoint, fun) {
    geoc.getLocation(mPoint, function (rs) {
        if (typeof fun == 'function') {
            fun(rs.surroundingPois);
        }
    }); //getGeolocation
}


///////////////////////////////////////////////
/////////////////map//////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////